﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace PSE.Framework.UI.Controls.SmartClient
{
    public static class DependencyObjectExtensionMethods
    {
        /// <summary>
        /// Atualiza a property que está com binding
        /// </summary>
        /// <param name="obj">Objeto com a property</param>
        /// <param name="property">Property a ser atualizada</param>
        /// <returns>true se atualizou com sucesso, false se property não for data bound</returns>
        public static bool UpdateTarget(this DependencyObject obj, DependencyProperty property)
        {
            if (property == null)
                throw new ArgumentNullException("property");

            if (BindingOperations.IsDataBound(obj, property))
	        {
                if (System.Threading.Thread.CurrentThread != obj.Dispatcher.Thread)
                {

                    obj.Dispatcher.Invoke(
                        new Action(
                                delegate()
                                {
                                    BindingOperations.GetBindingExpressionBase(obj, property).UpdateTarget();
                                }
                   ));
                }
                else
                {
                    BindingOperations.GetBindingExpressionBase(obj, property).UpdateTarget();
                }

                return true;
	        }
            else
	        {
                return false;                        
	        }
        }
    }
}
